package com.sgb.member.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.sgb.member.R
import com.sgb.member.databinding.HomeMemberMenuItemBinding
import com.sgb.member.entity.AppIcon

class HomeMemberMenuAdapter(val context:Context,var data: List<AppIcon>, val param:(AppIcon)->Unit) : RecyclerView.Adapter<HomeMemberMenuAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding: HomeMemberMenuItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context),
                R.layout.home_member_menu_item, parent, false);
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.image.setOnClickListener {
            param(data[position])
        }
        data.get(position).iconUrl?.let {
            Glide.with(context).load(it).into(holder.binding.image)
        }
        holder.binding.executePendingBindings()
    }

    override fun getItemCount(): Int = data?.size

    inner class ViewHolder(val binding: HomeMemberMenuItemBinding) : RecyclerView.ViewHolder(binding.getRoot())

}

